<?php
namespace other\email;
use other\ShopingConn;
use other\Table;
use think\Db;
use think\Exception;
use think\exception\PDOException;

/**
 * 根据条件 修改订单表的  is_meet_email_sending_rule
 */
class etimeEx extends etime
{
    /**
     * 操作的字段
     * @var string
     */
    private $field = 'is_meet_email_sending_rule';


    /**
     * 操作 表 orders 的 is_meet_email_sending_rule 字段
     * @return void
     * @throws Exception
     */
    public function do()
    {
        // 当前时间刻度
        $dscale_now = $this->_get_scale(time());

        // 满足刻度的订单id
        $one_ids = ShopingConn::getConn()->table(Table::Table_Order_Email_Time)->where('dscale_rule_one', $dscale_now)->column('order_id');
        $two_ids = ShopingConn::getConn()->table(Table::Table_Order_Email_Time)->where('dscale_rule_two', $dscale_now)->column('order_id');
        $three_ids = ShopingConn::getConn()->table(Table::Table_Order_Email_Time)->where('dscale_rule_three', $dscale_now)->column('order_id');
        $four_ids = ShopingConn::getConn()->table(Table::Table_Order_Email_Time)->where('dscale_rule_four', $dscale_now)->column('order_id');

        // 根据订单id 修改订单表的 状态
        Db::transaction(function ()use($one_ids,$two_ids,$three_ids,$four_ids){

            // 先重置
            ShopingConn::getConn()->table(Table::Table_Order)->where($this->field,1)->update([$this->field => 0]);

            $this->_is_meet_email_sending_rule($one_ids);
            $this->_is_meet_email_sending_rule($two_ids);
            $this->_is_meet_email_sending_rule($three_ids);
            $this->_is_meet_email_sending_rule($four_ids);

        });
    }

    /**
     * 修改一批订单的发送状态
     * @param $order_ids
     * @return void
     * @throws Exception
     * @throws PDOException
     */
    private function _is_meet_email_sending_rule($order_ids){
        if ($order_ids){
            foreach ($order_ids as $order_id){
                ShopingConn::getConn()->table(Table::Table_Order)->where('id',$order_id)->update([$this->field => 1]);
            }
        }
    }
}
